package demo4.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
@PropertySource("classpath:config.properties")
public class DruidBUtil implements IDBUtil{
    private DruidDataSource ds;//数据池
    public DruidBUtil(){
        ds = new DruidDataSource();
    }
    @Value("${spring.datasource.url}")
    public void setUrl(String url){
        ds.setUrl(url);
    }
    @Value("${spring.datasource.username}")
    public void setUsername(String username){
        ds.setUsername(username);
    }
    @Value("${spring.datasource.password}")
    public void setPassword(String password){
        ds.setPassword(password);
    }
    @Value("${spring.datasource.classname}")
    public void setClassname(String classname){
        ds.setDriverClassName(classname);
    }
    @Override
    public Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
